package com.huatai.vba.execute.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huatai.vba.common.utils.DateUtils;
import com.huatai.vba.common.utils.StringUtil;
import com.huatai.vba.dal.jdbc.DBUtils;
import com.huatai.vba.dal.jdbc.SqlUtils;
import com.huatai.vba.dal.model.VbaFileAction;
import com.huatai.vba.execute.action.vo.Hsfundid;
import com.huatai.vba.execute.utils.DBFConvertDBService;
import com.huatai.vba.properties.Dbconfig;

/**
 * 工行借口生成程序
 * 
 * @author fanrui16
 *
 */
@Service("icbcInitLoadDataActionExecutor")
public class IcbcInitLoadDataActionExecutor extends AbstractIcbcActionExecutor {

	private static final Logger logger = LoggerFactory.getLogger(IcbcInitLoadDataActionExecutor.class);

	@Autowired
	private DBFConvertDBService dBFConvertDBService;

	@Override
	public void execute(VbaFileAction action) {
		try {

			String yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String yyyy = yyyy_MM_dd.substring(0, 4);
			String yyyyMM = yyyy_MM_dd.substring(0, 7).replace("-", "");
			String yyMM = yyyy_MM_dd.substring(2, 7).replace("-", "");
			String yyMMdd = yyyy_MM_dd.substring(2).replace("-", "");
			String MMdd = yyyy_MM_dd.substring(5).replace("-", "");
			String MM = yyyy_MM_dd.substring(5, 7);
			String dd = yyyy_MM_dd.substring(8);

			// 复制文件
			String sjsmx1FilePath = "H:/vsat3/dbf/" + yyyy + "/" + yyMM + "/" + yyMMdd + "/sjsmx1" + MMdd + ".dbf";
			File srcFile = new File(sjsmx1FilePath);
			if (srcFile.exists()) {
				dBFConvertDBService.convertDBFToDB(sjsmx1FilePath, "sjsmx1");
				logger.info("加载数据文件：{}", srcFile.getName());
			}

			// 复制深交所发行库
			String sjsFileName = "H:/DCOM/sjsfx" + MMdd + ".dbf";
			File f = new File(sjsFileName);
			if (f.exists()) {
				dBFConvertDBService.convertDBFToDB(sjsFileName, "sjsfx");
				logger.info("加载数据文件：{}", "sjsfx.DBF");
			}
			// '复制上交所结算明细库
			String strFileTamp = DateUtils.convertExaMonth(MM) + dd;
			String sjsFile = "H:/remote/托管/" + yyyy + "/" + yyyyMM + "/" + yyMMdd + "/jsmxjs946." + strFileTamp;
			File f1 = new File(sjsFile);
			if (f1.exists()) {
				dBFConvertDBService.convertDBFToDB(sjsFile, "jsmxjs946");
				logger.info("加载数据文件：{}", sjsFile);
			}
			getHsTrasData(convertSql(strOne));
		} catch (Exception ex) {
			logger.info("系统异常", ex);
		}
	}

	private boolean getHsTrasData(String combi) throws Exception {
		vbaExtendsService.executeSql("delete from hsfundid");

		StringBuffer strSql = new StringBuffer();
		strSql.append(" select a.vc_deal_no deal_id,b.vc_fund_code fund_code,a.vc_report_seat seat,c.c_invest_type,");
		strSql.append(" (case when d.c_market_no = '2' and d.c_entrust_direction = '5' then");
		strSql.append(" '0S' when d.c_market_no = '2' and d.c_entrust_direction = '6' then");
		strSql.append(" '0B' Else D.VC_report_direction end) vc_report_direction, ");
		strSql.append(" d.c_market_no ");
		strSql.append(" from TREALDEAL a, Tfundinfo b, tentrusts c,tentrustdirection d");
		strSql.append(" where a.l_fund_id = b.l_fund_id And c.l_entrust_serial_no = a.l_entrust_serial_no");
		strSql.append(" and a.c_market_no=d.c_market_no and a.c_entrust_direction=d.c_entrust_direction");
		strSql.append(" and b.vc_fund_code in (" + combi + ") order by deal_id asc ");
		List<Map<String, Object>> list = DBUtils.executeQuery(Dbconfig.TRADEDB, user,
				Dbconfig.getPasswordByUserId(user), strSql.toString());

		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			Hsfundid hs = new Hsfundid();

			String tmpId = (String) map.get("DEAL_ID");
			String tmpCode = (String) map.get("FUND_CODE");
			String tmpSeat = (String) map.get("SEAT");
			// 20150126 席位发生变化，改为判断深交所席位，tmpid就补为8位
			if (tmpSeat.length() == 6 && tmpId.length() < 8) {
				tmpId = StringUtil.alignRights(tmpId, 8, "0");
			}
			hs.setCjbh(tmpId);
			hs.setCombi_id(getICBCCombi(tmpCode));
			hs.setInvesttype((String) map.get("c_invest_type".toUpperCase()));
			hs.setDirection((String) map.get("VC_report_direction".toUpperCase()));
			hs.setMarket_no((String) map.get("c_market_no".toUpperCase()));
			String insertSql = SqlUtils.generateSql(hs);
			vbaExtendsService.executeSql(insertSql);
		}
		return true;
	}

}
